package net.NingXiaRuiTai.ShortChain.config;

import lombok.Data;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@Data
public class RabbitMQErrorConfig {
    @Resource
    private RabbitTemplate rabbitTemplate;
    //异常交换机
    private String errorExchange = "short_link.error.exchange";
    //异常队列
    private String errorQueue = "short_link.error.queue";
    //异常路由键
    private String errorRoutingKey = "short_link.error.routing.key";

    /**
     * 异常交换机
     * @return
     */
    @Bean
    public TopicExchange errorExchange(){
        return new TopicExchange(errorExchange,true,false);
    }
    /**
     * 异常队列
     * @return
     */
    @Bean
    public Queue errorQueue(){
        return new Queue(errorQueue,true,false,false);
    }
    /**
     * 异常队列和交换机的绑定关系
     * @return
     */
    @Bean
    public Binding  BindingErrorQueueAndExchange(Queue errorQueue,TopicExchange errorExchange){
        return BindingBuilder.bind(errorQueue).to(errorExchange).with(errorRoutingKey);


    }
    /**
     * RepublishingMessageRecoverer
     *
     */
    @Bean
    public RepublishMessageRecoverer messageRecoverer(){
        return new RepublishMessageRecoverer(rabbitTemplate,errorExchange,errorRoutingKey);

    }

}
